// import store from '../store/index.js';	//vuex

// const url_all = {
// 	'DEV': 'http://192.168.31.112:8100',
// 	'BUILD': 'https://****api'1
// } // Request baseUrl
let server_url = 'http://192.168.31.106:8100';
//let server_url = 'http://192.168.31.112:8100';


//let server_url = 'http://localhost:8100';
// let token = '';

//process.env.NODE_ENV === 'development' ? server_url = url_all['DEV'] : server_url = url_all['BUILD'];


function service(options = {}) {
	var token = ""
	// store.state.token && (token = store.state.token);

	// Default configuration
	options.url = `${server_url}/api${options.url}`;


	options.data = options.params;
	options.filePath = options.filePath;

	return new Promise((resolved, rejected) => {

		options.success = (res) => {

			if (options.filePath != undefined) {
				let data = JSON.parse(res.data)
				if (data.code == 200) {
					let imageurl = server_url + data.data.network
					resolved(imageurl)
				} else {
					rejected(res.data.msg);
				}

			} else {
				let code = Number(res.data.code);

				// Do something with response data
				switch (code) {
					case 200:
						resolved(res.data.data)
						//resolved(res.data.data);
						break;
					case 402:
						rejected(res.data.msg);
						break;
					default:
						rejected(res.data.msg);
						break;
				}
			}
		}
		options.fail = (err) => {
			//Do something with request error
			rejected(err)
		}
		let token = uni.getStorageSync("token");

		if (token) {
			options.header = {
				"Authorization": "Wechat " + token

			};
			if (options.filePath != undefined) {
				uni.uploadFile(options)
			} else {
				uni.request(options);
			}
		} else {
			if (options.filePath != undefined) {
				uni.uploadFile(options)
			} else {
				uni.request(options);
			}
		}



	});
}
export default service;
